namespace DotNetNuke.UI.WebControls
{
    using System;
    using System.ComponentModel;
    using System.IO;
    using System.Web.Caching;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [ToolboxData("<{0}:CountryListBox runat=server></{0}:CountryListBox>")]
    public class CountryListBox : DropDownList
    {
        private bool _CacheGeoIPData = true;
        private string _GeoIPFile;
        private string _LocalhostCountryCode;
        private string _TestIP;

        protected override void OnDataBinding(EventArgs e)
        {
            bool IsLocal = false;
            if (!this.Page.IsPostBack)
            {
                string IP;
                if (this._GeoIPFile == "")
                {
                    this._GeoIPFile = "controls/CountryListBox/Data/GeoIP.dat";
                }
                this.EnsureChildControls();
                if (this._TestIP != "")
                {
                    IP = this._TestIP;
                }
                else if (this.Page.Request.UserHostAddress == "127.0.0.1")
                {
                    IsLocal = true;
                    IP = this.Page.Request.UserHostAddress;
                }
                else
                {
                    IP = this.Page.Request.UserHostAddress;
                }
                if ((this.Context.Cache.Get("GeoIPData") == null) & this._CacheGeoIPData)
                {
                    this.Context.Cache.Insert("GeoIPData", CountryLookup.FileToMemory(this.Context.Server.MapPath(this._GeoIPFile)), new CacheDependency(this.Context.Server.MapPath(this._GeoIPFile)));
                }
                if (IsLocal & (this._LocalhostCountryCode != ""))
                {
                    base.OnDataBinding(e);
                    if (this.Items.FindByValue(this._LocalhostCountryCode) != null)
                    {
                        this.Items.FindByValue(this._LocalhostCountryCode).Selected = true;
                    }
                }
                else
                {
                    CountryLookup _CountryLookup;
                    if (this._CacheGeoIPData)
                    {
                        _CountryLookup = new CountryLookup((MemoryStream) this.Context.Cache.Get("GeoIPData"));
                    }
                    else
                    {
                        _CountryLookup = new CountryLookup(this.Context.Server.MapPath(this._GeoIPFile));
                    }
                    string _UserCountryCode = _CountryLookup.LookupCountryCode(IP);
                    base.OnDataBinding(e);
                    if (this.Items.FindByValue(_UserCountryCode) != null)
                    {
                        this.Items.FindByValue(_UserCountryCode).Selected = true;
                    }
                    else
                    {
                        string _UserCountry = _CountryLookup.LookupCountryName(IP);
                        if (_UserCountry != "N/A")
                        {
                            ListItem newItem = new ListItem();
                            newItem.Value = _UserCountryCode;
                            newItem.Text = _UserCountry;
                            this.Items.Insert(0, newItem);
                            this.Items.FindByValue(_UserCountryCode).Selected = true;
                        }
                    }
                }
            }
        }

        [Category("Caching"), Bindable(true), DefaultValue(true)]
        public bool CacheGeoIPData
        {
            get
            {
                return this._CacheGeoIPData;
            }
            set
            {
                this._CacheGeoIPData = value;
                if (!value)
                {
                    this.Context.Cache.Remove("GeoIPData");
                }
            }
        }

        [Category("Appearance"), Bindable(true), DefaultValue("")]
        public string GeoIPFile
        {
            get
            {
                return this._GeoIPFile;
            }
            set
            {
                this._GeoIPFile = value;
            }
        }

        [Bindable(true), DefaultValue(""), Category("Appearance")]
        public string LocalhostCountryCode
        {
            get
            {
                return this._LocalhostCountryCode;
            }
            set
            {
                this._LocalhostCountryCode = value;
            }
        }

        [DefaultValue(""), Category("Appearance"), Bindable(true)]
        public string TestIP
        {
            get
            {
                return this._TestIP;
            }
            set
            {
                this._TestIP = value;
            }
        }
    }
}

